
package carlyang;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class FacadeUtils {
    private static boolean isDebug = true;
    private static final String MODULE_TAG = "Facade";

//    public static void log(String tag, String msg) {
//        if (isDebug) {
//            System.out.println(MODULE_TAG +" : "+msg);
//        }
//    }
    public static <E> void log(String tag, E msg) {
        if (isDebug) {
            System.out.println(tag +" : "+msg);
        }
    }
    public static void log(String tag, String msg, Throwable tr) {
        if (isDebug) {
        	System.out.println(tag +" : "+ msg + tr);
        }
    }

    public static byte[] getByetsFromFile(File file) {
        byte[] buffer = null;
        ByteArrayOutputStream bos = null;

        try {
            FileInputStream fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            buffer = new byte[1024];
            int length = 0;
            while ((length = fis.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
            }
            fis.close();
            bos.close();
            return bos.toByteArray();// 瀛楄妭娴佽浆鎹负涓�涓� byte鏁扮粍
        } catch (Exception e) {
            log(MODULE_TAG, "Read Node Failed");
        }
        return null;
    }

    public static String getStringFromPath(String path) {
        File file = new File(path);
        byte[] bytes = getByetsFromFile(file);
        String str = new String(bytes);
        return str;
    }

    public static void writeString(String path,String str){
    	File file = new File(path);
    	try {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(str.getBytes());
			fos.close();
		}catch (Exception e) {
			log(MODULE_TAG, "Write Node Failed");
		}
    }

    public static ExecutorService newFixedThreadPool(int nThreads) {

        return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
    }
    public static void threadDelay(int time) {
    	log(MODULE_TAG, "threadDelay:"+time+"ms,thread name:"+Thread.currentThread().getName()+",id="+Thread.currentThread().getId());
    	try {
			Thread.currentThread().sleep(time);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
